import $ from 'n-zepto';
export default {
    data(){
      return{
        pageNum:1,   //当前页面
        pageSize:10, //页面大小
        shelveCount: 0, //上架个数
        //专题列表
        subjectList: [],
        dataTotal:0,  //列表项
        noticekey:'noticekey1', //Notice弹窗name
        showWarnModal:false, //格式校验提示框
        warnContent:'',  //提示文案
        thisSubId:'', //当前操作专题id
        thisSubStatus:'',
        subObject:'',
        showConfirmModal:false, //格式校验提示框
        confirmContent:'',  //提示文案
      }
    },
  mounted () {
    this.init();
  },
    methods: {
      init: function(){
        var _this = this;
        var param = {
            pageNum: this.pageNum,
            pageSize: this.pageSize
        };
        var scrollTop = this.$util.sessionData("scrollTop")|| 0;
        $(document.body).scrollTop(scrollTop);
        this.$util.sessionData("scrollTop","")
        this.querySubjectList(param);
      },
      //根据当前页获得列表页数据
      pageNumChange: function(currrentPage){
        var _this = this;
        this.pageNum = currrentPage;
        var param = {
          pageNum:currrentPage,
          pageSize: this.pageSize
        };
        this.querySubjectList(param);
      },
      querySubjectList: function(param){
        var _this = this;
        _this.$http.post('convSubjectManage/getSubjectManageList',param)
          .then(function(resData){
            if(resData.meta.code == '0'){
              let data=resData.data;
              for(var i=0;i< data.result.length;i++){
                data.result[i].number = i+1;
                data.result[i].imgshow = false;
              }
              _this.shelveCount = data.shelveCount;
              _this.subjectList = data.result;
              _this.dataTotal = data.dataTotal;
            }else{
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: resData.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = resData.meta.message;
            }
          })
          .catch(function(err){
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: err.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      },
      //新增页跳转
      addSubject: function(){
        var scrollTop = $(document.body).scrollTop();
        this.$util.sessionData("scrollTop",scrollTop)
        this.$router.push({
          name: 'subjectNew',
          query:{
            shelveCount: this.shelveCount
          }
        })
      },
      //编辑页跳转
      updateSubject:function(id){
          var scrollTop = $(document.body).scrollTop();
        this.$util.sessionData("scrollTop",scrollTop)
        this.$router.push({
          name: 'subjectEdit' ,
          query:{
            subjectId: id,
            shelveCount: this.shelveCount
          }
        })
      },
      //专题详情页跳转
      querySubjectDetail:function(id){
        var scrollTop = $(document.body).scrollTop();
        this.$util.sessionData("scrollTop",scrollTop)
        this.$router.push({
          name: 'subjectDetail',
          query:{
              subjectId: id,
            shelveCount: this.shelveCount
          }
        })
      },
      //上移
      shiftUpSubject: function(param){
        var _this = this;
        var params = {
          subjectId: param.subjectId
        }
        this.$Notice.close(_this.noticekey);
        _this.$http.post('convSubjectManage/shiftUpSubject',param)
          .then(function(resData){
            if(resData.meta.code == '0'){
              _this.$Notice.success({
                title: ' ',
                desc:'处理成功',
                name:_this.noticekey,
                duration:2,
              });
              _this.init();
            }else{
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: resData.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = resData.meta.message;
            }
          })
          .catch(function(err){
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   // content: err.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      },
      //上下架
      shelveOrUnshelveSubject: function(item){
        var _this = this;
        this.thisSubId = item.subjectId;
        this.thisSubStatus = item.status== 0? 1: 0;
        this.subObject = item;
        var params={
          subjectId: item.subjectId,
          isShelve: item.status== 0? 1: 0
        }
        if( item.status==1){
          switch(this.shelveCount){
            case 1:
                  // this.$Modal.confirm({
                  //   title: '提示',
                  //   content: "<p>下架此专题后将没有专题在首页显示，请确认是否下架！</p>",
                  //   onOk: ()=>{
                  //     _this.queryShelveOrUnshelveSubject(params,item);
                  //   }
                  // })
                  this.showConfirmModal = true;
                  this.confirmContent = "下架此专题后将没有专题在首页显示，请确认是否下架！";
                  break;
             default:
               // this.$Modal.confirm({
               //   title: '提示',
               //   content: "<p>下架后此专题将不在专题首页显示，请确认是否下架！</p>",
               //   onOk: ()=>{
               //     _this.queryShelveOrUnshelveSubject(params,item);
               //   }
               // })
               this.showConfirmModal = true;
               this.confirmContent = "下架后此专题将不在专题首页显示，请确认是否下架！";

          }
        }else{
          if(this.shelveCount==5){
            // this.$Modal.warning({
            //   title: '提示',
            //   content: "轮播展示最多有5个专题，请下架后再将此专题上架！"
            // });
            _this.showWarnModal = true;
            _this.warnContent = "轮播展示最多有5个专题，请下架后再将此专题上架！";
          }else{
            // this.$Modal.confirm({
            //   title: '提示',
            //   content: "<p>上架后此专题将在专题首页显示，请确认是否上架！</p>",
            //   onOk: ()=>{
            //     _this.queryShelveOrUnshelveSubject(params,item);
            //   }
            // })
            this.showConfirmModal = true;
            this.confirmContent = "上架后此专题将在专题首页显示，请确认是否上架！";

          }
        }

      },
      queryShelveOrUnshelveSubject: function(params,item){
        var _this = this;
        this.$Notice.close(_this.noticekey);
        _this.$http.post('convSubjectManage/shelveOrUnshelveSubject',params)
          .then(function(resData){
            _this.showConfirmModal = false;
            if(resData.meta.code == '0'){
              _this.$Notice.success({
                title: ' ',
                desc:'处理成功',
                name:_this.noticekey,
                duration:2,
              });
              item.status=item.status== 0? 1: 0;
              _this.init();

            }else{
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: resData.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = resData.meta.message;
            }
          })
          .catch(function(err){
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   // content: err.meta.message
            // });
            _this.showConfirmModal = false;
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      },
      closeModal(){
        this.showWarnModal = false;
        this.showConfirmModal = false;
      },
      closeConfirmModal(){
        this.showConfirmModal = false;
        this.showWarnModal = false;
      }
    }

}
